package com.seveneight.baijiesteward.http;

import android.content.Context;
import android.util.Log;

import com.alibaba.fastjson.JSONArray;
import com.seveneight.baijiesteward.utils.JsonUtils;
import com.seveneight.baijiesteward.utils.LogUtil;
import com.seveneight.baijiesteward.utils.MD5Parse;
import com.seveneight.baijiesteward.utils.PreferencesUtil;
import com.seveneight.baijiesteward.utils.Setting;
import com.seveneight.baijiesteward.utils.StringUtils;

import org.json.JSONObject;

import java.util.Map;
import java.util.Set;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by user1 on 2018/3/30.
 */

public class JsonFormat {
    private static JsonFormat format;
    private Context context;

    public synchronized static JsonFormat getInstance(Context context) {
        if (format == null) {
            format = new JsonFormat(context);
        }
        return format;
    }

    public JsonFormat(Context context) {
        this.context = context;
    }


    /**
     * 升级用户等级
     *
     * @param UpGradeRankID
     * @param CardType
     * @param BankCardId
     * @param TradePassword
     * @return
     */
    public RequestBody upGrade(String UpGradeRankID, String CardType, String BankCardId, String TradePassword) {
        JSONObject object = new JSONObject();
        try {
            object.put("UserID", Setting.getInstance(context).getUserID());
            object.put("UpGradeRankID", UpGradeRankID);
            object.put("CardType", CardType);
            object.put("BankCardId", BankCardId);
            object.put("TradePassword", MD5Parse.parseStr(TradePassword));
            object.put("Signature", MD5Parse.finalKey(object));
        } catch (Exception ex) {

        }
        return requestBodyPrase(object);
    }


    public RequestBody teamList(int page, int type) {
        JSONObject object = new JSONObject();
        try {
            object.put("UserID", Setting.getInstance(context).getUserID());
            object.put("Page", page);
            object.put("TeamType", type);
            object.put("Rows", 20);
            object.put("Signature", MD5Parse.finalKey(object));
        } catch (Exception ex) {

        }
        return requestBodyPrase(object);
    }


    /**
     * 收益列表
     *
     * @param page
     * @return
     */
    public RequestBody profitList(int page) {
        JSONObject object = new JSONObject();
        try {
            object.put("UserID", Setting.getInstance(context).getUserID());
            object.put("Page", page);
            object.put("Rows", 20);
            object.put("Signature", MD5Parse.finalKey(object));
        } catch (Exception ex) {

        }
        return requestBodyPrase(object);
    }


    /**
     * 获取订单列表
     *
     * @param OrderType
     * @return
     */

    public RequestBody orderList(String OrderType, int page) {
        JSONObject object = new JSONObject();
        try {
            object.put("UserID", Setting.getInstance(context).getUserID());
            object.put("OrderType", OrderType);
            object.put("Page", page);
            object.put("Rows", 20);
            object.put("Signature", MD5Parse.finalKey(object));
        } catch (Exception ex) {

        }
        return requestBodyPrase(object);
    }

    /**
     * 创建订单
     *
     * @param Amount
     * @param InCardType
     * @param InCardId
     * @param OutCardType
     * @param OutCardId
     * @param TradePassword
     * @return
     */

    public RequestBody createOrder(String Amount, String InCardType, String InCardId, String OutCardType, String OutCardId, String TradePassword, int OrderType) {
        JSONObject object = new JSONObject();
        try {
            object.put("UserID", Setting.getInstance(context).getUserID());
            object.put("Amount", Amount);
            object.put("InCardType", InCardType);
            object.put("InCardId", InCardId);
            object.put("OutCardType", OutCardType);
            object.put("OutCardId", OutCardId);
            object.put("TradePassword", MD5Parse.parseStr(TradePassword));
            object.put("OrderType", OrderType);
            object.put("Signature", MD5Parse.finalKey(object));
        } catch (Exception ex) {

        }
        return requestBodyPrase(object);
    }

    /**
     * 移除信用卡
     *
     * @param CreditCardID
     * @return
     */

    public RequestBody setCreditCard(String CreditCardID) {
        JSONObject object = new JSONObject();
        try {
            object.put("UserID", Setting.getInstance(context).getUserID());
            object.put("CreditCardID", CreditCardID);
            object.put("Signature", MD5Parse.finalKey(object));
        } catch (Exception ex) {

        }
        return requestBodyPrase(object);
    }

    /**
     * 删除、设置默认借记卡
     *
     * @param DebitCardID
     * @return
     */

    public RequestBody setDebitCard(String DebitCardID) {
        JSONObject object = new JSONObject();
        try {
            object.put("UserID", Setting.getInstance(context).getUserID());
            object.put("DebitCardID", DebitCardID);
            object.put("Signature", MD5Parse.finalKey(object));
        } catch (Exception ex) {

        }
        return requestBodyPrase(object);
    }

    /**
     * 添加借记卡
     *
     * @param BankCardNo
     * @param BankId
     * @param BankPhone
     * @param IsDefault
     * @return
     */

    public RequestBody addDebitCard(String RealName, String BankId, String BankCardNo, String BankPhone, int IsDefault, String BranchBankName, String BranchBankCode) {
        JSONObject object = new JSONObject();
        try {
            object.put("UserID", Setting.getInstance(context).getUserID());
            object.put("RealName", RealName);
            object.put("BankID", BankId);
            object.put("DebitCardNo", BankCardNo);
            object.put("IdCardNo", Setting.getInstance(context).getUserInfo().getIdCardNo());
            object.put("BankPhone", BankPhone);
            object.put("IsDefault", IsDefault);
            object.put("BranchBankName", BranchBankName);
            object.put("BranchBankCode", BranchBankCode);
            object.put("Signature", MD5Parse.finalKey(object));
        } catch (Exception ex) {

        }
        return requestBodyPrase(object);
    }

    /**
     * 修改借记卡
     *
     * @param BankCardNo
     * @param BankId
     * @param BankPhone
     * @param IsDefault
     * @return
     */

    public RequestBody editDebitCard(String DebitCardId, String RealName, String BankId, String BankCardNo, String BankPhone, int IsDefault, String BranchBankName, String BranchBankCode) {
        JSONObject object = new JSONObject();
        try {
            object.put("DebitCardId", DebitCardId);
            object.put("UserID", Setting.getInstance(context).getUserID());
            object.put("RealName", RealName);
            object.put("BankID", BankId);
            object.put("DebitCardNo", BankCardNo);
            object.put("IdCardNo", Setting.getInstance(context).getUserInfo().getIdCardNo());
            object.put("BankPhone", BankPhone);
            object.put("IsDefault", IsDefault);
            object.put("BranchBankName", BranchBankName);
            object.put("BranchBankCode", BranchBankCode);
            object.put("Signature", MD5Parse.finalKey(object));
        } catch (Exception ex) {

        }
        return requestBodyPrase(object);
    }


    /**
     * 更新登录密码
     *
     * @param AuthCode
     * @param Password
     * @return
     */

    public RequestBody updateLoginPwd(String Mobile, String AuthCode, String Password) {
        JSONObject object = new JSONObject();
        try {
            object.put("Mobile", Mobile);
            object.put("AuthCode", AuthCode);
            object.put("Password", MD5Parse.parseStr(Password));
            object.put("Signature", MD5Parse.finalKey(object));
        } catch (Exception ex) {

        }
        return requestBodyPrase(object);
    }

    /**
     * 获取支行和支行联行号接口
     *
     * @param QueryType
     * @param ProvinceId
     * @param CityId
     * @param BankId
     * @return
     */
    public RequestBody getBranchBank(int QueryType, String ProvinceId, String CityId, String BankId) {
        JSONObject object = new JSONObject();
        try {
            object.put("UserID", Setting.getInstance(context).getUserID());
            object.put("QueryType", QueryType);
            if (ProvinceId != null) {
                object.put("ProvinceId", ProvinceId);
            }
            if (CityId != null) {
                object.put("CityId", CityId);
            }
            if (BankId != null) {
                object.put("BankId", BankId);
            }
            object.put("Signature", MD5Parse.finalKey(object));
        } catch (Exception ex) {

        }
        return requestBodyPrase(object);
    }

    /**
     * 更新交易密码
     *
     * @param AuthCode
     * @param Password
     * @return
     */

    public RequestBody updateTracePwd(String AuthCode, String Password) {
        JSONObject object = new JSONObject();
        try {
            object.put("UserID", Setting.getInstance(context).getUserID());
            object.put("AuthCode", AuthCode);
            object.put("Password", MD5Parse.parseStr(Password));
            object.put("Signature", MD5Parse.finalKey(object));
        } catch (Exception ex) {

        }
        return requestBodyPrase(object);
    }

    /**
     * 更新用户头像
     *
     * @param HeadImgUrl
     * @return
     */

    public RequestBody updateHead(String HeadImgUrl) {
        JSONObject object = new JSONObject();
        try {
            object.put("UserID", Setting.getInstance(context).getUserID());
            object.put("HeadImgUrl", HeadImgUrl);
            object.put("Signature", MD5Parse.finalKey(object));
        } catch (Exception ex) {

        }
        return requestBodyPrase(object);
    }

    /**
     * 更新用户信息
     *
     * @param Email
     * @param Address
     * @return
     */

    public RequestBody updateInfo(String Email, String Address) {
        JSONObject object = new JSONObject();
        try {
            object.put("UserID", Setting.getInstance(context).getUserID());
            object.put("Email", Email);
            object.put("Address", Address);
            object.put("Signature", MD5Parse.finalKey(object));
        } catch (Exception ex) {

        }
        return requestBodyPrase(object);
    }

    /**
     * 实名认证
     *
     * @param RealName
     * @param IdCardNo
     * @param BankMobile
     * @param BankCardNo
     * @param BankId
     * @param IdCardImgUrl
     * @param BankCardImgUrl
     * @param HoldIdCardImgUrl
     * @return
     */

    public RequestBody identify(String RealName, String IdCardNo, String BankMobile, String BankCardNo, String BankId, String IdCardImgUrl, String BankCardImgUrl, String HoldIdCardImgUrl) {
        JSONObject object = new JSONObject();
        try {
            object.put("UserID", Setting.getInstance(context).getUserID());
            object.put("RealName", RealName);
            object.put("IdCardNo", IdCardNo);
            object.put("BankMobile", BankMobile);
            object.put("BankCardNo", BankCardNo);
            object.put("BankId", BankId);
            object.put("IdCardImgUrl", IdCardImgUrl);
            object.put("BankCardImgUrl", BankCardImgUrl);
            object.put("HoldIdCardImgUrl", HoldIdCardImgUrl);
            object.put("Signature", MD5Parse.finalKey(object));
        } catch (Exception ex) {

        }
        return requestBodyPrase(object);
    }

    /**
     * 获取验证码
     *
     * @param mobile
     * @param type
     * @return
     */
    public RequestBody getAuthCode(String mobile, String type) {
        JSONObject object = new JSONObject();
        try {
            object.put("Mobile", mobile);
            object.put("AuthCodeType", type);
            object.put("Signature", MD5Parse.finalKey(object));
        } catch (Exception ex) {

        }
        return requestBodyPrase(object);
    }

    /**
     * 只需要用户id的通用json
     *
     * @return
     */
    public RequestBody commonParam() {
        JSONObject object = new JSONObject();
        try {
//            if (!StringUtils.isEmpty(Setting.getInstance(context).getUserID())){
                object.put("UserID", Setting.getInstance(context).getUserID());
//            }else {
//                object.put("UserID", PreferencesUtil.get(context,PreferencesUtil.userId,""));
//            }
            object.put("Signature", MD5Parse.finalKey(object));
            Log.i("requstData:",object.toString());
        } catch (Exception ex) {

        }
        return requestBodyPrase(object);

    }

    /**
     * 只需要用户id的通用json
     *
     * @return
     */
    public RequestBody getUserInfo(String id) {
        JSONObject object = new JSONObject();
        try {
            object.put("UserID", id);
            object.put("Signature", MD5Parse.finalKey(object));
        } catch (Exception ex) {

        }
        return requestBodyPrase(object);
    }

    /**
     * 登录
     *
     * @param mobile
     * @param pwd
     * @param authcode
     * @return
     */
    public RequestBody login(String mobile, String pwd, String authcode) {
        JSONObject object = new JSONObject();
        try {
            object.put("Mobile", mobile);
            object.put("Password", MD5Parse.parseStr(pwd));
            object.put("Signature", MD5Parse.finalKey(object));
        } catch (Exception ex) {

        }
        return requestBodyPrase(object);
    }
    /**
     * 修改信用卡日期
     * @return
     */
    public RequestBody updateCardDate(String billDate, String repayDate,String creditCardId) {
        JSONObject object = new JSONObject();
        try {
            object.put("CreditCardId", creditCardId);
            object.put("BillDate", Integer.parseInt(billDate));
            object.put("RepayDate", Integer.parseInt(repayDate));
            object.put("UserID", Setting.getInstance(context).getUserID());
            object.put("Signature", MD5Parse.finalKey(object));
        } catch (Exception ex) {

        }
        Log.i("requstData:",object.toString());
        return requestBodyPrase(object);
    }
    /**
     * 新加信用卡
     * @return
     */
    public RequestBody AddCard(Map<String,String> params) {
        JSONObject object = new JSONObject();

        try {
            // 获取所有键值对对象的集合
            Set<Map.Entry<String, String>> set = params.entrySet();
            // 遍历键值对对象的集合，得到每一个键值对对象
            for (Map.Entry<String, String> me : set) {
                // 根据键值对对象获取键和值
                String key = me.getKey();
                String value = me.getValue();
                object.put(key, value);
            }
            object.put("UserID", Setting.getInstance(context).getUserID());
            object.put("Signature", MD5Parse.finalKey(object));
        } catch (Exception ex) {

        }
        Log.i("requstData:",object.toString());
        return requestBodyPrase(object);
    }
    /**
     * 新加信用卡
     * @return
     */
    public RequestBody AddObject(Map<String,Object> params) {
        JSONObject object = new JSONObject();

        try {
            // 获取所有键值对对象的集合
            Set<Map.Entry<String, Object>> set = params.entrySet();
            // 遍历键值对对象的集合，得到每一个键值对对象
            for (Map.Entry<String, Object> me : set) {
                // 根据键值对对象获取键和值
                String key = me.getKey();
                Object value = me.getValue();
                object.put(key, value);
            }
            object.put("UserID", Setting.getInstance(context).getUserID());
            object.put("Signature", MD5Parse.finalKey(object));
        } catch (Exception ex) {

        }
        Log.i("requstData:",object.toString());
        return requestBodyPrase(object);
    }
    /**
     * 新加信用卡
     * @return
     */
    public RequestBody AddCardBank(Map<String,Object> params) {
        JSONObject object = new JSONObject();

        try {
            object.put("UserID", Setting.getInstance(context).getUserID());
            object.put("Signature", MD5Parse.finalKey(object));
            // 获取所有键值对对象的集合
            Set<Map.Entry<String, Object>> set = params.entrySet();
            // 遍历键值对对象的集合，得到每一个键值对对象
            for (Map.Entry<String, Object> me : set) {
                // 根据键值对对象获取键和值
                String key = me.getKey();
                Object value = me.getValue();
                object.put(key, value);
            }
        } catch (Exception ex) {

        }
        Log.i("requstData:",object.toString());
        return requestBodyPrase(object);
    }
    /**
     * 修改信用卡类型
     * @return
     */
    public RequestBody updateRepayData(Map<String,String> params,org.json.JSONArray dateList) {
        JSONObject object = new JSONObject();

        try {
            // 获取所有键值对对象的集合
            Set<Map.Entry<String, String>> set = params.entrySet();
            // 遍历键值对对象的集合，得到每一个键值对对象
            for (Map.Entry<String, String> me : set) {
                // 根据键值对对象获取键和值
                String key = me.getKey();
                String value = me.getValue();
                object.put(key, value);
            }
            object.put("UserID", Setting.getInstance(context).getUserID());
            object.put("Signature", MD5Parse.finalKey(object));
            object.put("PlanDetailList",dateList);
        } catch (Exception ex) {

        }
        Log.i("requstData:",object.toString());
        return requestBodyPrase(object);
    }
    /**
     * 新加信用卡
     * @return
     */
    public String params(Map<String,String> params) {
        JSONObject object = new JSONObject();

        try {
            // 获取所有键值对对象的集合
            Set<Map.Entry<String, String>> set = params.entrySet();
            // 遍历键值对对象的集合，得到每一个键值对对象
            for (Map.Entry<String, String> me : set) {
                // 根据键值对对象获取键和值
                String key = me.getKey();
                String value = me.getValue();
                object.put(key, value);
            }
            object.put("UserID", Setting.getInstance(context).getUserID());
            object.put("Signature", MD5Parse.finalKey(object));
        } catch (Exception ex) {

        }
        Log.i("requstData:",object.toString());
        return object.toString();
    }
    /**
     * 升级
     *
     * @return
     */
    public RequestBody GradeCode(String code) {
        JSONObject object = new JSONObject();
        try {
            object.put("RedeemCode", code);
            if (!StringUtils.isEmpty(Setting.getInstance(context).getUserID())){
                object.put("UserID", Setting.getInstance(context).getUserID());
            }else {
                object.put("UserID", PreferencesUtil.get(context,PreferencesUtil.userId,""));
            }
            object.put("Signature", MD5Parse.finalKey(object));
        } catch (Exception ex) {

        }
        return requestBodyPrase(object);
    }

    public RequestBody requestBodyPrase(JSONObject object) {
        RequestBody body = RequestBody.create(MediaType.parse("UTF-8"), object.toString());
        return body;
    }
}
